package com.xmm.view;

import javax.swing.tree.DefaultMutableTreeNode;

import com.xmm.constants.CONSTANTS;
import com.xmm.model.Music;

public class MusicTreeNode extends DefaultMutableTreeNode {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6228907901777534410L;
	
	private int mType = CONSTANTS.NODE_TYPE_ROOT;
	private Music mMusic = null;
	
	public MusicTreeNode(String name) {
		super(name);
		mType = CONSTANTS.NODE_TYPE_ROOT;
	}
	
	public MusicTreeNode(int type, String name) {
		super(name);
		mType = type;
	}
	
	public String getCodeId() {
		if(mMusic == null) return null;
		return mMusic.getCodeId();
	}
	
	public int getType() {
		return mType;
	}
	
	public void setMusic(Music music) {
		mMusic = music;
	}
	public Music getMusic() {
		return mMusic;
	}
	
	public boolean isLibrary() {
		return (mType == CONSTANTS.NODE_TYPE_LIBRARY);
	}
	public boolean isAblum() {
		return (mType == CONSTANTS.NODE_TYPE_ABLUM);
	}
	public boolean isTrack() {
		return (mType == CONSTANTS.NODE_TYPE_TRACK);
	}
	
	public Music removeFromNode(String codeId) {
		if(mMusic == null) return null;
		Music ret = mMusic.removeFrom(codeId);
		assert(getChildCount() == mMusic.getClidrenCount());
		return ret;
	}
	
	public static MusicTreeNode getMusicTreeNode(DefaultMutableTreeNode node) {
		if (node == null) {
			return null;
		}
		Object obj = node.getUserObject();
		if(obj instanceof MusicTreeNode) {
			return (MusicTreeNode)obj;
		} else {
			return null;
		}
	}
}
